﻿using Sustainalytics.DataLayer;
using Sustainalytics.Entities;
using Sustainalytics.Services.Admin.EventsService;
using Sustainalytics.Services.Admin.WebApi.DTOs;
using Sustainalytics.Utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace Sustainalytics.Services.Admin.WebApi.Controllers
{
    /// <summary>
    /// News controller.
    /// </summary>
    public class EventsController : ApiController, IEventsController
    {
        private IEventsManagement _eventsManager { get; set; }

        /// <summary>
        /// Ctor.
        /// </summary>
        public EventsController()
        {
            // init news repo
            IEventsRepository eventsRepo = EventsRepository.GetEventsRepository(ConfigurationManager.ConnectionStrings["MongoDbDataSqlConnectionString"].ConnectionString);

            // init news mgr
            IEventsManagement eventsMgr = EventsManagement.GetInstance(eventsRepo);

            _eventsManager = eventsMgr;
        }

        public EventsController(IEventsManagement eventsManager)
        {
            _eventsManager = eventsManager;
        }


        ///// <summary>
        ///// Get news filtered.
        ///// </summary>
        ///// <param name="pageIndex"> Page index. </param>
        ///// <param name="pageSize"> Page size. </param>
        ///// <returns> Paged list of news. </returns>
        //[HttpGet]
        //[Route("api/news/paged")]
        //public PagedList<NewsDto> GetNewsPaged(string term = null, int? category = null, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        //{
        //    PagedList<News> newsPaged = this.NewsManager.GetNewsPaged(
        //        filter: new NewsFilter { Term = term, Category = category.HasValue ? (NewsCategory)category.Value : (NewsCategory?)null }, 
        //        pagingOptions: new PagingOptions { PageIndex = pageIndex, PageSize = pageSize },
        //        orderBy: orderBy);

        //    PagedList<NewsDto> newsDtoPaged = newsPaged.To<NewsDto>(n => new NewsDto().LoadFrom(n));

        //    return newsDtoPaged;
        //}

        ///// <summary>
        ///// Get news by ID.
        ///// </summary>
        ///// <param name="newsId"> News ID. </param>
        ///// <returns> News. </returns>
        //[HttpGet]
        //[Route("api/news/{newsId}")]
        //public NewsDto GetNews(Guid newsId)
        //{
        //    News news = this.NewsManager.GetNews(newsId);

        //    if (news == null)
        //        return null;

        //    NewsDto newsDto = new NewsDto();
        //    newsDto.LoadFrom(news);

        //    return newsDto;
        //}


        ///// <summary>
        ///// Add news.
        ///// </summary>
        ///// <param name="news"> News data. </param>
        ///// <returns> Added news. </returns>
        //[HttpPost]
        //[Route("api/news")]
        //public NewsDto AddNews(NewsDto news)
        //{
        //    if (news == null)
        //        throw CustomExceptionBuilder.News.NewsIsNull();

        //    News newsEnt = new News();
        //    newsEnt.AddedByUserName = User == null || User.Identity == null ? null : User.Identity.Name;
        //    news.SaveToNew(newsEnt);

        //    News newNews = this.NewsManager.AddNews(newsEnt);

        //    news.LoadFrom(newNews);

        //    return news;
        //}

        ///// <summary>
        ///// Update news.
        ///// </summary>
        ///// <param name="news"> News data. </param>
        ///// <returns> Updated news. </returns>
        //[HttpPut]
        //[Route("api/news")]
        //public NewsDto UpdateNews(NewsDto news)
        //{
        //    if (news == null)
        //        throw CustomExceptionBuilder.News.NewsIsNull();

        //    News newsEnt = new News();
        //    news.SaveToExisting(newsEnt);

        //    News newNews = this.NewsManager.UpdateNews(newsEnt);

        //    news.LoadFrom(newNews);

        //    return news;
        //}

        ///// <summary>
        ///// Delete news items.
        ///// </summary>
        //[HttpDelete]
        //[Route("api/news/{newsId}")]
        //public void DeleteNews(Guid newsId)
        //{
        //    this.NewsManager.DeleteNews(newsId);
        //}

        ///// <summary>
        ///// Publish multiple news.
        ///// </summary>
        ///// <param name="newsItems"> News items to publish. </param>
        //[HttpPut]
        //[Route("api/news")]
        //public void PublishMultipleNews([FromBody] IEnumerable<Guid> newsItems, [FromUri] bool publish)
        //{
        //    if (!publish)
        //        return;

        //    if (newsItems == null)
        //        throw new ArgumentNullException("newsItems");

        //    this.NewsManager.PublishMultipleNews(newsItems);
        //}

        ///// <summary>
        ///// Accepts a batch of operations to perform on the resource.
        ///// </summary>
        ///// <param name="patchData"> Batch operations. </param>
        //[AcceptVerbs("PATCH")]
        //[Route("api/news")]
        //public void PatchNews([FromBody]NewsPatch patchData)
        //{
        //    this.NewsManager.PatchNews(patchData);
        //}
        public EventsDto AddEvent(EventsDto eventObj)
        {
            throw new NotImplementedException();
        }

        public void DeleteEvent(Guid eventId)
        {
            throw new NotImplementedException();
        }

        public EventsDto GetEvent(Guid eventId)
        {
            throw new NotImplementedException();
        }

        public PagedList<EventsDto> GetEventsPaged(string term = null, int? category = null, int pageIndex = 1, int pageSize = 20,
            string orderBy = null)
        {
            throw new NotImplementedException();
        }

        public void PublishMultipleEvents(IEnumerable<Guid> eventsItems, bool publish)
        {
            throw new NotImplementedException();
        }

        public EventsDto UpdateEvent(EventsDto eventsObj)
        {
            throw new NotImplementedException();
        }
    }
}
